#  Name:		acpi
#  Version:		0.7
#  Part Of:		linuxrules

#  Description: Main acpi functions
#               This is getting apparently bloated,
#               but it's just better! ;)
#				* temperature sensors
#				* battery status and charge
#				* LID state
#				* screen brightness
#               * battery time expectancy until discharging
#               * AC Adapter state

#  Modified:	Mon Oct 25 2010, 09:46:18
#  See COPYING for linuxrules GPL3 licensing info.

#  NOTICE:	I HAVE REDUCED DELIBERATELY THE NUMBER OF DEVICES.
#			THIS NUMBERS SHOULD BE ENOUGH FOR MOST MACHINES !?
#           BATTERY LIFE CALCULATION IS _RATHER LOOSE_ AND
#           _PESSIMISTIC BY DESIGN_.

function convert ( number ) {
# output in human form and turn
# non significant part to hours range.
    dec = number - int(number)

    if (dec == 0)
    {
        if (number < 10)
            number = "0" number ":00"
        else
            number = number ":00"
    }
    else
        if (dec < number)
        {
            number = number - dec
            dec = dec * 60 / 100

            if (number < 10)
                number = "0" sprintf("%.2f", number + dec)
            else
                number = "" sprintf("%.2f", number + dec)

            sub(/\./, ":", number)
        }
        else
        {
            number = "0" sprintf("%.2f", number / 1.2)
            sub(/\./, ":", number)
        }

    return number
}

BEGIN {
    r = p = 1

	getline < "/proc/acpi/button/lid/LID/state"
	_acpi["LID"] = $2
	close("/proc/acpi/button/lid/LID/state")

    getline < "/proc/acpi/ac_adapter/ACAD/state"
    ac_state = $2
    _acpi["ACAD"] = ac_state
    close("/proc/acpi/ac_adapter/ACAD/state")
	
    # Detect until 4 monitors (secuentially linked).
	for ( bright = 0; bright < 4; bright++ ) {
		if (getline level < ("/sys/devices/virtual/backlight/acpi_video" bright "/brightness") > 0)
			_acpi["SCREENBR", bright] = level
		close("/sys/devices/virtual/backlight/acpi_video" bright "/brightness")
	}
	
    # Detect until 3 batteries. Eats about 2 miliseconds more for the whole Linuxrules
	for (  batts  = 0;  batts  < 3;  batts ++ ) {
		if (getline < ("/sys/class/power_supply/BAT" batts "/energy_full") > 0)  # old format
			batt_full = $0
		close("/sys/class/power_supply/BAT" batts "/energy_full")

		if (getline < ("/sys/class/power_supply/BAT" batts "/charge_full") > 0)  # new format
			batt_full = $0
		close("/sys/class/power_supply/BAT" batts "/charge_full")

		if (getline < ("/sys/class/power_supply/BAT" batts "/status") > 0)
        {
			_acpi["BATTST", batts] = $0
            # Things get complicated when we try to guess a particular index
            # for a /proc battery state as we can't rely on AWK's secondary
            # effects. Thus, an index record has to be provided in order to
            # print data accurately.
            found_batts[batts] = 1
        }
		close("/sys/class/power_supply/BAT" batts "/status")

		if (getline < ("/sys/class/power_supply/BAT" batts "/energy_now") > 0)  # old format
			_acpi["BATTCH", batts] = int($0 / batt_full * 100)
		close("/sys/class/power_supply/BAT" batts "/energy_now")

		if (getline < ("/sys/class/power_supply/BAT" batts "/charge_now") > 0)  # new format
			_acpi["BATTCH", batts] = int($0 / batt_full * 100)
		close("/sys/class/power_supply/BAT" batts "/charge_now")

	}

    if (ac_state ~ /off/)
    {
        for (idx in found_batts) {
            while ((getline < ("/proc/acpi/battery/BAT" idx "/state")) > 0)
            {
                if ( $1 ~ /remaining/ )
                    r = $3
                if ( $0 ~ /present rate:/ )
                    p = $3
                batteries[idx] = (r / p)
            }
        }
        close("/proc/acpi/battery/BAT" idx "/state")
        _acpi["BATTREM", idx] = convert(batteries[idx])
    }
}

# vim:ft=awk:
